package parcebibletext;

import java.io.*;
import java.util.List;
import java.util.Scanner;

/**
 * gets raw bible text and stores proccessed bible text.
 * @author Shavy Yarmush
 */
public class BibleTextIO 
{
    final String INPUT_DIR;
    final String OUTPUT_DIR;
    
    public BibleTextIO()
    {
        String dir = System.getProperty("user.dir");
        INPUT_DIR = dir + "\\Tanach\\input\\";
        OUTPUT_DIR = dir + "\\Tanach\\output\\";
    }  
    
    public void processAllTexts(Parcer bd, String fileNameFilter)
    {
        
        for (String textFile : getDirItems(INPUT_DIR, fileNameFilter))
        {
            String bibleText = getBookText( INPUT_DIR + textFile);
            
            bibleText = bd.convertText(bibleText);
            
            writeReadyFiles(bibleText, OUTPUT_DIR + textFile, bd.getArgs());
        }
    }
    
    public void processAllTexts(Parcer db)
    {
        processAllTexts( db, "");
    }
    
    static String[] getDirItems(String dir, final String fileNameFilter)
    {
       File file = new File(dir);
        
       return file.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) 
            {
                if(fileNameFilter.length() > 0)
                    return name.endsWith(".txt") && name.contains(fileNameFilter);
                else
                    return name.endsWith(".txt");
            }
        });
    }
    
    static String getBookText(String bookLoc) 
    {
        String bibleText = "";
        File file = new File(bookLoc);
        
        if(!file.exists())
            throw new RuntimeException(bookLoc + " does not exist.");
        
        try (Scanner scan = new Scanner(file)) 
        {
            if(!scan.hasNextLine())
                throw new RuntimeException(bookLoc + " is empty.");
            
            while(scan.hasNextLine())
            {
                bibleText = bibleText + scan.nextLine() + " ";
            }
        }
        catch (FileNotFoundException e)
        {
            throw new RuntimeException("Couldnt find file that exists: " + e.getMessage(),e);
        }
        
        if(bibleText.trim().isEmpty())
            throw new RuntimeException(bookLoc + " is empty (after access).");
        
        return bibleText;
    }
    
    void writeReadyFiles(String bibleText, String fileLoc, List<String> args) 
    {
        fileLoc = fileLoc.substring(0, fileLoc.length()-4) + Parcer.argsToString(args)+".txt";
        
        try 
        {
            File file = new File(fileLoc);

            file.createNewFile();

            try (FileWriter fw = new FileWriter(file)) {
                fw.write(bibleText);
                fw.flush();
            }
        }
        catch(IOException e)
        {
            throw new RuntimeException("Couldnt create new file: " + e.getMessage(),e);
        }
    }
}
